package com.changpay.zywy.ui.fragment.main

import android.view.View
import android.widget.ProgressBar
import android.widget.TextView
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseMvpFragment
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.mvp.contract.IncomeGroupContract
import com.changpay.zywy.bean.EarningGroupBean
import com.changpay.zywy.mvp.presenter.IncomeGroupPresenter
import com.changpay.zywy.utils.DateUtil
import com.changpay.zywy.utils.StringUtil
import kotlinx.android.synthetic.main.fragment_income_group.*
import java.util.*


class IncomeGroupFragment : BaseMvpFragment<IncomeGroupContract.View, IncomeGroupContract.Presenter>(),
    IncomeGroupContract.View {


    private var start_date = ""
    private var end_date = ""
    private var selected_date = ""
    private var date_type = "week"
    private var date_type_index = 1
    private val default_week = 1
    private val default_month = 2
    private val search_week = 4
    private val search_month = 5
    private val dayViewList = mutableListOf<TextView>()
    private val DataViewList = mutableListOf<TextView>()
    private val progressViewList = mutableListOf<ProgressBar>()

    private val dayViewMonthList = mutableListOf<TextView>()
    private val DataViewMonthList = mutableListOf<TextView>()
    private val progressViewMonthList = mutableListOf<ProgressBar>()


    override fun showEarning(data: EarningGroupBean) {
        setEarningData(data)
    }


    companion object {
        fun getInstance(): IncomeGroupFragment = IncomeGroupFragment()
    }

    override fun createPresenter(): IncomeGroupContract.Presenter = IncomeGroupPresenter()

    override fun attachLayoutRes(): Int = R.layout.fragment_income_group

    override fun lazyLoad() {
        initDateData(date_type_index, "")
    }

    override fun initView(view: View) {
        super.initView(view)
        income_group_date_rg.setOnCheckedChangeListener { group, checkedId ->
            if (selected_date.isEmpty()) {
                when (checkedId) {
                    R.id.income_group_week_rb -> initDateData(default_week, selected_date)
                    R.id.income_group_month_rb -> initDateData(default_month, selected_date)
                }
            } else {
                when (checkedId) {
                    R.id.income_group_week_rb -> initDateData(search_week, selected_date)
                    R.id.income_group_month_rb -> initDateData(search_month, selected_date)
                }
            }
        }

        // 周控件
        dayViewList.add(day_one)
        dayViewList.add(day_two)
        dayViewList.add(day_three)
        dayViewList.add(day_four)
        dayViewList.add(day_five)
        dayViewList.add(day_six)
        dayViewList.add(day_seven)

        DataViewList.add(data_one)
        DataViewList.add(data_two)
        DataViewList.add(data_three)
        DataViewList.add(data_four)
        DataViewList.add(data_five)
        DataViewList.add(data_six)

        progressViewList.add(progress_one)
        progressViewList.add(progress_two)
        progressViewList.add(progress_three)
        progressViewList.add(progress_four)
        progressViewList.add(progress_five)
        progressViewList.add(progress_six)
        progressViewList.add(progress_seven)


        // 月份控件
        dayViewMonthList.add(day_one_month)
        dayViewMonthList.add(day_two_month)
        dayViewMonthList.add(day_three_month)
        dayViewMonthList.add(day_four_month)
        dayViewMonthList.add(day_five_month)

        DataViewMonthList.add(data_one_month)
        DataViewMonthList.add(data_two_month)
        DataViewMonthList.add(data_three_month)
        DataViewMonthList.add(data_four_month)
        DataViewMonthList.add(data_five_month)
        DataViewMonthList.add(data_six_month)

        progressViewMonthList.add(progress_one_month)
        progressViewMonthList.add(progress_two_month)
        progressViewMonthList.add(progress_three_month)
        progressViewMonthList.add(progress_four_month)
        progressViewMonthList.add(progress_five_month)

    }

    private fun loadData() {
        val param = hashMapOf<String, String>()
        param["key"] = token
        param["type"] = "3"
        param["date_type"] = date_type
        param["search_date_end"] = end_date
        mPresenter?.getEarning(param)
    }

    private fun setEarningData(data: EarningGroupBean) {
        val dateList = mutableListOf<String>()  // 日期list
        val numberList = mutableListOf<Double>()  // 数据list
        val xDataList = mutableListOf<String>()
        val percentList = mutableListOf<Int>()  // 百分比list
        // 其中一个为空时,默认请求
        data.let {
            // 1.获取日期
            val datas = data.earnings_info.group_earnings
            datas.forEach { data ->
                dateList.add(data.date)
                numberList.add(data.earnings.toDouble())
            }
            // 求出最大值
            var max = 0.0
            var total = 0.0  // 算出来的总数
            numberList.forEach { data ->
                if (data > max) max = data
                total += data
            }
            // 判断最大值不同的情况
            when (max) {
                0.0 -> {
                    for (i in 0..5) {
                        xDataList.add((i * 2).toString())
                    }
                    numberList.forEach { _ -> percentList.add(0) }
                }
                in 0..10 -> {
                    for (i in 0..5) {
                        xDataList.add((i * 2).toString())
                    }
                    numberList.forEach { percentList.add((it / max).toInt()) }
                }
                else -> {
                    val base = max.toInt() / 5
                    for (i in 0..5) {
                        xDataList.add((i * base).toString())
                    }
                    numberList.forEach { percentList.add((it / max).toInt()) }
                }
            }

            if (date_type_index==default_week||date_type_index==search_week)
            {
                // 设置行坐标
                for (i in 0 until numberList.size) {
                    dayViewList[i].text = dateList[i]
                    progressViewList[i].progress = percentList[i]
                }
                // 设置纵坐标
                for (i in 0 until xDataList.size) {
                    DataViewList[i].text = xDataList[i]
                }
            }else
            {

                // 设置行坐标
                for (i in 0 until numberList.size) {
                    dayViewMonthList[i].text = dateList[i]
                    progressViewMonthList[i].progress = percentList[i]
                }
                // 设置纵坐标
                for (i in 0 until xDataList.size) {
                    DataViewMonthList[i].text = xDataList[i]
                }
            }


            // 设置总数
            income_group_tv.text = "￥${StringUtil.formatDouble(total)}"
        }


    }


    fun setTime(selectTime: String) {
        selected_date = selectTime
        when (date_type_index) {
            default_month, search_month -> initDateData(search_month, selected_date)
            default_week, search_week -> initDateData(search_week, selected_date)
        }
    }


    fun initDateData(type: Int, date: String) {
        date_type_index = type
        when (type) {
            default_week -> {
                rv_data_month.visibility = View.GONE
                rv_data_week.visibility = View.VISIBLE
                date_type = "week"
                val NowDate = DateUtil.getPastDate(0)
                val pastDate = DateUtil.getPastDate(6)
                start_date = date
                end_date = date
                income_data_tv.text = ("$pastDate 至 $NowDate")
                loadData()
            }
            default_month -> {
                rv_data_month.visibility = View.VISIBLE
                rv_data_week.visibility = View.GONE
                date_type = "month"
                start_date = date
                end_date = date
                val instance = Calendar.getInstance()
                val firstDayOfMonth1 =
                    DateUtil.getFirstDayOfMonth1(instance.get(Calendar.YEAR), instance.get(Calendar.MONTH))
                val lastDayOfMonth1 =
                    DateUtil.getLastDayOfMonth1(instance.get(Calendar.YEAR), instance.get(Calendar.MONTH))
                income_data_tv.text = "$firstDayOfMonth1 至 $lastDayOfMonth1"
                loadData()

            }
            search_week -> {
                rv_data_month.visibility = View.GONE
                rv_data_week.visibility = View.VISIBLE
                date_type = "week"
                start_date = DateUtil.getSpecifiedDayBefore(date, 6)
                end_date = date
                income_data_tv.text = "$start_date 至 $end_date"
                loadData()
            }
            search_month -> {
                rv_data_month.visibility = View.VISIBLE
                rv_data_week.visibility = View.GONE
                date_type = "month"
                val split = date.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
                if (split.size != 3) {
                    showToast("获取时间错误")
                    return
                }
                start_date = DateUtil.getFirstDayOfMonth1(Integer.valueOf(split[0]), Integer.valueOf(split[1]) - 1)
                end_date = DateUtil.getLastDayOfMonth1(Integer.valueOf(split[0]), Integer.valueOf(split[1]) - 1)
                income_data_tv.text = "$start_date 至 $end_date"
                loadData()
            }
            else -> {
                rv_data_month.visibility = View.GONE
                rv_data_week.visibility = View.VISIBLE
                date_type = "week"
                val NowDate = DateUtil.getPastDate(0)
                val pastDate = DateUtil.getPastDate(6)
                start_date = date
                end_date = date
                income_data_tv.text = ("$pastDate 至 $NowDate")
                loadData()
            }

        }
    }
}